package com.chuangke.sinotrukbudget.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.core.http.HttpResult;
import com.chuangke.sinotrukbudget.model.BudgetAccount;
import com.chuangke.sinotrukbudget.service.BudgetAccountService;

/**
 * @author chuangke
 * @date 2022-10-20
 */
@RestController
@RequestMapping("budget/account")
public class BudgetAccountController {

	@Autowired
	private BudgetAccountService budgetAccountService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody BudgetAccount record) {
		budgetAccountService.save(record);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<BudgetAccount> records) {
		budgetAccountService.delete(records);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findtree")
	public HttpResult findTree() {
		return HttpResult.ok(budgetAccountService.findTree());
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/tree")
	public HttpResult tree(@RequestBody Map<String,Object> params) {
		String fuzzy = (String)params.get("name") ;
		return HttpResult.ok(budgetAccountService.findTree(fuzzy));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findlastlevel")
	public HttpResult findLastLevelDept() {
		return HttpResult.ok(budgetAccountService.findLastLevel());
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/findlevel/{level}")
	public HttpResult findLevelDept(@PathVariable("level") int level) {
		return HttpResult.ok(budgetAccountService.findByLevel(level));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/level")
	public HttpResult level() {
		budgetAccountService.level();
		return HttpResult.ok();
	}

}
